package multithreading;

/**
 * @author DuJiabao
 * @create 2020-12-13 18:56
 */
public class SaleTicket4 {
    public static void main(String[] args) {
        Window4 w = new Window4();
//       三个线程同一个目标
        Thread thread1 = new Thread(w);
        Thread thread2 = new Thread(w);
        Thread thread3 = new Thread(w);

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Window4 implements Runnable {
    private int total = 10000;
//    Object object = new Object();

    @Override
    public void run() {
        while (show());
    }

    private synchronized boolean show() {
        // 必须先判断，再执行，不然会出问题的
        if (total <= 0)
            return false;
        System.out.println(Thread.currentThread().getName() + ": " + total--);
        return true;
    }
}

